package com.copa.l202;

/**
 * @author copa
 * @createDate 2022-08-29 16:34
 * @function 快慢指针法
 */
public class Test2 {

    public boolean isHappy(int n) {
        int slow = getNext(n);
        int fast = getNext(getNext(n));
        while (slow != fast || slow != 1) {
            if (slow == 1) {
                return true;
            }
            if (fast == 1) {
                return true;
            }
            if (slow == fast) {
                return false;
            }
            slow = getNext(slow);
            fast = getNext(getNext(fast));
        }
        return true;
    }

    private int getNext(int n) {
        int temp = 0;
        char[] chars = String.valueOf(n).toCharArray();
        for (char c : chars) {
            temp += Math.pow(c - '0', 2);
        }
        return temp ;
    }

    public static void main(String[] args) {
        System.out.println(new Test2().isHappy(1));
    }
}
